<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <title>添加小知识</title>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>
    <meta name="renderer" content="webkit"/>
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <link rel="icon" type="image/png" href="assets/common/i/favicon.ico"/>
    <meta name="apple-mobile-web-app-title" content="添加小知识"/>
    <link rel="stylesheet" href="Css/amazeui.min.css"/>
    <link rel="stylesheet" href="Css/app.css"/>
    <script src="Scripts/api.js"></script>
    <link rel="stylesheet" href="Css/font_783249_3hfdb7m5w0j.css">
    <script src="Scripts/jquery.min.js"></script>
    <script src="Scripts/font_783249_e5yrsf08rap.js"></script>
    <script>
        BASE_URL = 'https://demo.yiovo.com/';
        STORE_URL = 'index.php?s=/store';
    </script>
    <script>

    </script>
</head>

<body data-type="">
<div class="am-g tpl-g">
    <!-- 内容区域 start -->
    <div class="row">
        <div class="am-u-sm-12 am-u-md-12 am-u-lg-12">
            <div class="widget am-cf">
                <form class="am-form tpl-form-line-form" id="my-form" method="post" novalidate="novalidate">
                    <div class="widget-body">
                        <fieldset>
                            <div class="widget-head am-cf">
                                <div class="widget-title am-fl">基本信息</div>

                            </div>
                            <from id="my-from">
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">小知识标题 </label>
                                    <div class="am-u-sm-9 am-u-end">
                                        <input name="title" class="tpl-form-input" id="addtitle" required="" type="text"
                                               value="">
                                    </div>
                                </div>

                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">小知识图片 </label>
                                    <div class="am-u-sm-9 am-u-end">
                                        <div class="am-form-file">
                                            <div class="am-form-file">
                                                <input name="file" id="imagefile" type="file" accept="image/*"
                                                       onchange="selectImage(this);" class="imageButton"/>
                                                <button class="upload-file am-btn am-btn-secondary am-radius"
                                                        type="button">
                                                    选择图片
                                                </button>
                                                <div class="uploader-list am-cf">
                                                </div>
                                            </div>


                                        </div>
                                        <div>

                                        </div>
                                        <img id="image" src=""/>
                                    </div>
                                </div>
                                <div class="am-form-group">
                                    <label class="am-u-sm-3 am-u-lg-2 am-form-label form-require">小知识内容 </label>
                                    <div class="am-u-sm-9 am-u-end">
                                        <input name="content" class="v" type="text" id="knowlegeC" value="">
                                        <small></small>
                                    </div>

                                </div>
                            </from>
                            <div class="am-form-group">
                                <div class="am-u-sm-9 am-u-sm-push-3 am-margin-top-lg">
                                    <button class="j-submit am-btn am-btn-secondary" type="button"
                                    >提交
                                    </button>
                                    <button class="j-back am-btn am-btn-secondary" type="button"
                                    >返回
                                    </button>
                                </div>
                            </div>
                            <div class="am-form-group">

                            </div>
                        </fieldset>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <script>
        var isUpdate = false;//是否是更新小知识页面的标识
        var isImageChange = false;//是否更改小知识图片
        var isAddImage = false;

        //图片处理
        var image = '';

        function selectImage(file) {
            if (!file.files || !file.files[0]) {
                return;
            }
            var reader = new FileReader();
            reader.onload = function (evt) {
                document.getElementById('image').src = evt.target.result;
                image = evt.target.result;
                isImageChange = true;
                isAddImage = true;
            }
            reader.readAsDataURL(file.files[0]);
        }



        $('.j-back').click(function (){
            window.location.href = "/admin/knowledge.html";
        })
        //获得网址中的参数
        var getParam = function (name) {
            var search = document.location.search;
            var pattern = new RegExp("[?&]" + name + "\=([^&]+)", "g");
            var matcher = pattern.exec(search);
            var items = null;
            if (null != matcher) {
                try {
                    items = decodeURIComponent(decodeURIComponent(matcher[1]));
                } catch (e) {
                    try {
                        items = decodeURIComponent(matcher[1]);
                    } catch (e) {
                        items = matcher[1];
                    }
                }
            }
            return items;
        };
        //获得要修改小知识的ID
        var wxapphelpid = getParam('id');
        var oldcreat_time;

        var ImagePath = null;
        var callbackImage =  function (wxhelpdata) {
            ImagePath = wxhelpdata;
        };
        var addImage =function () {
            document.getElementById('image').src = ImagePath;
            image = ImagePath;
            isAddImage = true;
            ImagePath =null;
        }

        var callbackKnowledge = function (wxhelpdata) {
            isUpdate = true;
            var oldtitle = JSON.stringify(wxhelpdata['title']).replace(/"/g, "");
            var oldcontent = JSON.stringify(wxhelpdata['content']).replace(/"/g, "");
            oldcreat_time = JSON.stringify(wxhelpdata['createTime']);

            $("#addtitle").val(oldtitle);
            $("#knowlegeC").val(oldcontent);
        };

        if (wxapphelpid != null) {
            getKnowledgeImage(wxapphelpid,callbackImage);
            addImage();
            getKnowledgeById(wxapphelpid,callbackKnowledge);

        }




        var callbackT = function () {
            alert("提交成功！");
        }
        //提交表单
        $('.j-submit').click(function () {

            var title = $("#addtitle").val();
            var content = $("#knowlegeC").val();
            if (title == "") {
                layer.confirm('请填写小知识标题！'
                    , {title: '友情提示'}
                    , function (index) {
                        layer.close(index);
                    }, function () {
                        layer.close();
                    });
            } else if (content == "") {
                layer.confirm('请填写小知识内容！'
                    , {title: '友情提示'}
                    , function (index) {
                        layer.close(index);
                    }, function () {
                        layer.close();
                    });
            } else if (!isAddImage) {
                layer.confirm('请添加小知识图片！'
                    , {title: '友情提示'}
                    , function (index) {
                        layer.close(index);
                    }, function () {
                        layer.close();
                    });

            } else {
                if (isUpdate) {
                    var formData = new FormData();
                    formData.append("id", wxapphelpid);
                    formData.append("title", title);
                    formData.append("content", content);
                    formData.append("file", $('#imagefile')[0].files[0]);
                    formData.append("type",1);
                    updateKnowledge(formData,callbackT);
                    window.location.href = "/admin/knowledge.html";

                } else {
                    var formData = new FormData();
                    formData.append("title", title);
                    formData.append("content", content);
                    formData.append("type",1);
                    formData.append("file", $('#imagefile')[0].files[0]);
                    addKnowledge(formData,callbackT);
                    window.location.href = "/admin/knowledge.html";

                }
            }
        })



    </script>
    <!-- 内容区域 end -->
</div>

<script src="Scripts/layer.js"></script>
<script src="Scripts/jquery.form.min.js"></script>
<script src="Scripts/amazeui.min.js"></script>
<script src="Scripts/webuploader.html5only.js"></script>
<script src="Scripts/art-template.js"></script>
<script src="Scripts/app.js"></script>
<script src="Scripts/file.library.js"></script>


</body>
</html>